﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SpawenerScript : MonoBehaviour {

	public GameObject enemyTrain;
	public GameObject groundTroop;
	public GameObject truck;
	public Vector2 AreaBounds = new Vector2(30.0f, 30.0f);


	public int numOfEnemies;
	public int curNumOfEnemies;

	float waveTimer = 0.0f;
	float waveTimerMax = 30.0f;

	int waveNumber = 0;

	public GameObject GUIbarPrefab;

	//Variables for the health gui bar creation
	GameObject waveBar;
	[HideInInspector]
	public GUIbar waveBarScript;
	public Texture2D waveBarTexture;
	public Texture2D waveBarimage;
	public Vector2 waveBarPosition;
	//this is the generic creation function for all the gui bars
	public void CreateWavebar()
	{
		waveBar = Instantiate (GUIbarPrefab)as GameObject;
		waveBarScript = waveBar.GetComponent<GUIbar> ();
		waveBarScript.SetFillTexture (waveBarTexture);
		waveBarScript.SetSymbolTexture (waveBarimage);
		waveBarScript.SetPosition (waveBarPosition);
	}

	//Variables for the health gui bar creation
	GameObject timerBar;
	[HideInInspector]
	public GUIbar timerBarScript;
	public Texture2D timerBarTexture;
	public Texture2D timerBarImage;
	public Vector2 timerBarPosition;
	//this is the generic creation function for all the gui bars
	public void CreateTimerbar()
	{
		timerBar = Instantiate (GUIbarPrefab)as GameObject;
		timerBarScript = timerBar.GetComponent<GUIbar> ();
		timerBarScript.SetFillTexture (timerBarTexture);
		timerBarScript.SetSymbolTexture (timerBarImage);
		timerBarScript.SetPosition (timerBarPosition);
	}


	// Use this for initialization
	void Start ()
	{
		CreateWavebar ();
        CreateTimerbar();

		SetNewWave ();

	}


	void SetNewWave()
	{

			waveNumber ++;
			for(int i = 0; i < waveNumber + 2; i++)
			{
				CreateEnemy(groundTroop, Vector3.zero);
			}
			if(waveNumber > 3)
			{
				for(int i= 0; i < (waveNumber/3); i++)
				{
					CreateEnemy(truck, Vector3.zero);
				}
			}
			for(int i = 0; i < 1+ (waveNumber/2); i++)
			{
				CreateEnemy(enemyTrain, Vector3.zero);
			}

			waveTimer = 0.0f;
			waveTimerMax += 15.0f;

		numOfEnemies = curNumOfEnemies;
		waveBarScript.SetBarPercent ((float)curNumOfEnemies / (float)numOfEnemies * 100.0f);
	}


	public void CreateEnemy (GameObject type, Vector3 position)
	{
		Vector3 startPos;
		if(position == Vector3.zero)
			startPos = new Vector3 (Random.Range (-AreaBounds.x, AreaBounds.x), 1, Random.Range (-AreaBounds.y, AreaBounds.y));
		else
			startPos = position;

		GameObject instance = Instantiate (type, startPos, Quaternion.identity) as GameObject;
		instance.GetComponent<BaseEnemy> ().mySpawner = this;
		curNumOfEnemies ++;
	}

	public void EnemyDied()
	{
		curNumOfEnemies --;
		waveBarScript.SetBarPercent (((float)curNumOfEnemies / (float)numOfEnemies) * 100.0f);
	}


	// Update is called once per frame
	void Update ()
	{
		waveTimer += Time.deltaTime;
		if(waveTimer>waveTimerMax || curNumOfEnemies ==0)
		{
			SetNewWave();
		}
		timerBarScript.SetBarPercent ((waveTimer/waveTimerMax )* 100.0f);
	}
}
